package be.groept.ace.enquete.model.beans;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import be.groept.ace.enquete.model.enums.Language;
import be.groept.ace.enquete.model.interfaces.PersistentieValideerbaar;
import be.groept.ace.enquete.utils.ServletUtils;
import be.groept.ace.enquete.utils.interfaces.FormValideerbaar;

public class Survey implements Comparable<Survey>, Serializable, FormValideerbaar, PersistentieValideerbaar {
	private static final long serialVersionUID = 1L;
	private static final int MIN_NUMBER_QUESTION = 3;
	private int id = -1;
	private int groupID = -1;
	private Date creationdate;
	private Date startdate;
	private Date enddate;
	private String title;
	private String description;
	private Language language;
	
	private User creator;
	private SortedSet<UserSurvey> userSurveySet;
	private SortedSet<Question> questionSet;
	
	public Survey() {
		userSurveySet = new TreeSet<UserSurvey>();
		questionSet = new TreeSet<Question>();
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getGroupID() {
		return groupID;
	}
	public void setGroupID(int groupID) {
		this.groupID = groupID;
	}
	public Date getCreationdate() {
		return creationdate;
	}
	public void setCreationdate(Date creationdate) {
		this.creationdate = creationdate;
	}
	
	
	
	public Date getStartdate() {
		return startdate;
	}
	public void setStartdate(Date startdate) {
		this.startdate = startdate;
	}
	public Date getEnddate() {
		return enddate;
	}
	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Language getLanguage() {
		return language;
	}
	public void setLanguage(Language language) {
		this.language = language;
	}
	
	public User getCreator(){
		return creator;
	}
	
	public void setCreator(User creator) {
		this.creator = creator;
	}
	
	public void addUserSurvey(UserSurvey userSurvey) {
		userSurveySet.add(userSurvey);
	}
	
	public void removeUserSurvey(UserSurvey userSurvey) {
		userSurveySet.remove(userSurvey);
	}
	
	public SortedSet<UserSurvey> getUserSurveySet() {
		return userSurveySet;
	}
	
	public boolean containsUserSurvey(UserSurvey userSurvey) {
		return userSurveySet.contains(userSurvey);
	}
	
	public void addQuestion(Question question) {
		questionSet.add(question);
	}
	
	public void removeQuestion(Question question) {
		questionSet.remove(question);
	}
	
	public SortedSet<Question> getQuestionSet() {
		return questionSet;
	}
	
	public boolean containsQuestion(Question question) {
		return questionSet.contains(question);
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if( getClass() != obj.getClass()) {
			return false;
		}
		Survey other = (Survey)obj;
		
		return getTitle().equals(other.getTitle());
	}
	
	@Override
	public int hashCode() {
		return getTitle().hashCode();
	}

	//compare by title asc
	@Override
	public int compareTo(Survey o) {
		return getTitle().compareTo(o.getTitle());
	}
	
	@Override
	public String toString() {
		return String.format("Survey object - id: %1$d - title: %2$s", getId(), getTitle());
	}

	@Override
	public boolean isValidForForm(List<String> errors) {
		// TODO Auto-generated method stub --> indien datums gevalideerd zijn de correcte velden initializeren
		boolean valid = true;
		if(ServletUtils.isNullOrEmpty(getTitle())) {
			errors.add("Titel mag niet leeg zijn");
			valid = false;
		}
		if(getLanguage() == null) {
			errors.add("Er moet een taal geselecteerd worden");
			valid = false;
		}
		Date date;
		try {
			date = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(getStartdateString());
			setStartdate(date);
		} catch (ParseException pe) {
			errors.add("Begindatum is niet in het juiste formaat");
			valid = false;
		}
		try {
			date = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(getEnddateString());
			setEnddate(date);
		} catch (ParseException pe) {
			errors.add("Einddatum is niet in het juiste formaat");
			valid = false;
		}
		return valid;
	}
	
	//extra velden om user input bij te houden -> wordt niet gepersisteerd
	private String startdateString;
	public String getStartdateString() {
		return startdateString;
	}
	
	public void setStartdateString(String startdateString) {
		this.startdateString = startdateString;
	}
	
	private String enddateString;
	public String getEnddateString() {
		return enddateString;
	}
	
	public void setEnddateString(String enddateString) {
		this.enddateString = enddateString;
	}

	@Override
	public boolean isValid(List<String> errors) {
		boolean valid = isValidForForm(errors);
		if(getQuestionSet().size() < MIN_NUMBER_QUESTION) {
			valid = false;
			errors.add("Enquete moet minstens " + MIN_NUMBER_QUESTION + " vragen bevatten: huidig aantal = " + getQuestionSet().size());
		}
		
		return valid;
	}
}
